package com.example.mall.dao;

import com.example.mall.model.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CommentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert(Comment comment) {
        String sql = "insert into comment (cID, aID, timestamp, content) values (?, ?, ?, ?)";
        jdbcTemplate.update(sql, comment.getcID(), comment.getaID(), comment.getTimestamp(), comment.getContent());
    }

    public void delete(Integer id) {
        String sql = "delete from comment where id = ?";
        jdbcTemplate.update(sql, id);
    }

    public List<Comment> getListByCid(Integer cID) {
        String sql = "select * from comment where cID = ? lock in share mode";
        RowMapper<Comment> rowMapper = new BeanPropertyRowMapper<>(Comment.class);
        return jdbcTemplate.query(sql, rowMapper, cID);
    }

}
